package tech.aistar.corejava.day13;

import java.io.*;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/2 0002
 */
public class BufferedDemo {
    public static void main(String[] args) {

        copyFile("src/tech/aistar/corejava/day13/FileDemo.java","src/tech/aistar/corejava/day13/FileDemo_副本.java");

    }

    public static void copyFile(String src,String target){
        //构建一个带缓存功能的文件字节输入流和输出流

        BufferedInputStream in = null;

        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(target));

            byte[] buf = new byte[3*1024];

            while(true){
                int len = in.read(buf);

                if(len == -1){
                    break;
                }

                //强制刷新缓冲区.
                out.write(buf,0,len);
                System.out.println("文件拷贝成功!");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            //只需要关闭包装流即可
            if(null!=in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
